/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package triseries;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Properties;

/**
 * @author tshepo
 *
 */
public class MyProperties {

	public static String[] nomRep;
	public static String[] nomRepDest;
	public static String research;
	public static String exts;
	public static String subs;
	public static boolean setDownloaded;
	public static boolean setWatched;
	public static boolean setSeeNotDled;
	public static boolean verbose;
	public static boolean noUI;
	public static int time;
	public static HashMap<String, String> titleRename;
	public static String arguments;
	public static long lastmodif;
	public static String debug = "";
	public static Properties configFile;

	/**
	 * Initialisation Récupère les réglages du fichier Settings.properties
	 */
	public static boolean init() {

		try {
			System.setErr(new PrintStream(new FileOutputStream("err.log",
					true)));
		} catch (FileNotFoundException ex) {
			Main.processExeption("MyProperties.init :\n"
				+ ex.getStackTrace().toString());
		}
		configFile = new Properties();
		try {
			if (!(new File("Settings.txt")).exists()) {
				FileAcces.ImportResource("Settings.txt",
						"/settings/Settings.Properties");
			}
			lastmodif = (new File("Settings.txt")).lastModified();
			configFile.load(new InputStreamReader(new FileInputStream(
					"Settings.txt"), "UTF8"));
		} catch (IOException ex) {
			Main.processExeption("MyProperties.init :\n"
				+ ex.getStackTrace().toString());
		}
		debug = configFile.getProperty("DEBUG", "false").trim();
		Main.debug = debug.equalsIgnoreCase("true");
		String key = "9528d3c8b6ad";

		String user = configFile.getProperty("user", "").trim();
		String password = configFile.getProperty("password", "").trim();

		if (user.isEmpty() || password.isEmpty()) {
			return false;
		}
		verbose = configFile.getProperty("verbose", "false").trim()
				.equalsIgnoreCase("true");
		noUI = configFile.getProperty("noUI", "false").trim().equalsIgnoreCase(
				"true");
		time = Integer.valueOf(configFile.getProperty("time"));
		if (time < 5)
			time = 60;
		String multiPath = configFile.getProperty("nomRep").trim();
		nomRep = multiPath.split(File.pathSeparator);
		multiPath = configFile.getProperty("nomRepDest").trim();
		nomRepDest = multiPath.split(File.pathSeparator);
		String format = configFile.getProperty("format", "{SERIE} - S{SAISON}"
				+ File.separator + "{SERIE} - {SAISON}x{EPISODE} - {TITRE}");
		boolean dots = configFile.getProperty("dots", "false").trim()
				.equalsIgnoreCase("true");
		setDownloaded = configFile.getProperty("setDownloaded", "false").trim()
				.equalsIgnoreCase("true");
		setWatched = configFile.getProperty("setWatched", "false").trim()
				.equalsIgnoreCase("true");
		setSeeNotDled = configFile.getProperty("setSeeNotDled", "true").trim()
				.equalsIgnoreCase("true");

		research = configFile.getProperty("WebResearch", "streaming").trim();

		if (Main.debug) {
			if (setDownloaded) {
				System.out.println("marquer les épisodes comme téléchargé");
			}
			if (setWatched) {
				System.out.println("marquer les épisodes comme vu");
			}
		}
		exts = configFile.getProperty("exts", "avi").trim();
		String saisonEtEpisodePattern = configFile.getProperty("pattern",
				"((S|s)[0-9]?[0-9](E|e)[0-9][0-9])").trim();
		subs = configFile.getProperty("dlSubtitle", "VF").trim();
		String filtreAvec = configFile.getProperty("Subtitlewith", ".*").trim();
		String filtreSans = configFile.getProperty("Subtitlewithout", "")
				.trim();
		arguments = configFile.getProperty("Subtitlemaybe", "").trim();

		String newtitle = configFile.getProperty("newtitle", "").trim();

		String[] tmpMU = newtitle.split(":");
		titleRename = new HashMap<String, String>();
		for (String mu : tmpMU) {
			String[] tmp = mu.split(">");
			if (tmp.length == 2) {
				titleRename.put(tmp[0], tmp[1]);
			}
		}

		if (!subs.equalsIgnoreCase("BOTH") && !subs.equalsIgnoreCase("VO")
				&& !subs.equalsIgnoreCase("VF"))
			subs = "NONE";

		VideoFile.SetParterns(exts, saisonEtEpisodePattern);
		VideoFile.SetFormat(format, dots);
		BetaSeriesXML.SetAutentification(key, user, password);
		BTSubtitles.setFiltres(filtreAvec, filtreSans);

		String proxyHost = configFile.getProperty("proxyHost", "").trim();
		String proxyPort = configFile.getProperty("proxyPort", "").trim();
		String proxyUser = configFile.getProperty("proxyUser", "").trim();
		String proxyPassword = configFile.getProperty("proxyPassword", "").trim();
		if(!proxyHost.isEmpty() && !proxyPort.isEmpty()) {
			System.setProperty("http.proxyHost", proxyHost);
			System.setProperty("http.proxyPort", proxyPort);
			System.setProperty("https.proxyHost", proxyHost);
			System.setProperty("https.proxyPort", proxyPort);
			if(!proxyUser.isEmpty()) {
				System.setProperty("http.proxyUser", proxyHost);
				System.setProperty("https.proxyUser", proxyPort);
			}
			if(!proxyPassword.isEmpty()) {
				System.setProperty("http.proxyPassword", proxyHost);
				System.setProperty("https.proxyPassword", proxyPort);
			}
		}	
		
		String iconWait = configFile.getProperty("iconWait", "").trim();
		String iconBusy = configFile.getProperty("iconBusy", "").trim();
		String iconError = configFile.getProperty("iconError", "").trim();
		if(!iconWait.isEmpty() && (new File(iconWait)).exists()) {
			Main.iconWait = java.awt.Toolkit.getDefaultToolkit()
			.getImage(iconWait);
		}
		if(!iconBusy.isEmpty() && (new File(iconBusy)).exists()) {
			Main.iconBusy = java.awt.Toolkit.getDefaultToolkit()
			.getImage(iconBusy);
		}
		if(!iconError.isEmpty() && (new File(iconError)).exists()) {
			Main.iconError = java.awt.Toolkit.getDefaultToolkit()
			.getImage(iconError);
		}
		
		MoveFile.override = configFile.getProperty("Override", "false").trim().equalsIgnoreCase(
		"true");
		
		return true;
	}
}
